﻿@{
    ViewBag.Title = "Manage Method (/m)";
}
<script type="text/javascript" src="/Scripts/jquery.validate.js"></script>
<script>
    var methodname = '@ViewData["name"]';
    var fullmethodname = 'usp_udt_@ViewData["name"]';
    var methodParamHtml = '<div class="param-entry"><div class="entry-name">##displayname##</div><div class="value"><input name="##inputname##" type="##type##" id="##inputname##" class="entry-input ##class##" ##validation## ></div></div>';

    $(document).ready(function () {
        if (methodname.length !== 0) {
            // Show loading animation
            //showLoadingAnimation($('#grid-container .grid-items-list'));
            getData('/m/sp_getProcedureParametersByName', 'procedurename=' + methodname, insertMethodParameters, false);
        }
        else {
            $('#grid-container').epicGrid({ contenttype: "dynamic" });
            $('#methodOptions').hide();
        }

        //$('.play-button').bind('click', makeMethodCall);
        $('#hideShowButton').bind('click', toggleRunMethodBox);
        $('#grid-container').epicGrid({ contenttype: "dynamic", globalButtonNames: ["filter"], refreshFunction: makeMethodCall });

        $("#methodCallString").bind("dblclick", openMethodCallInNewWindow);

        $('#methodOptions').validate({
            submitHandler: makeMethodCall
        });
    });

    function openMethodCallInNewWindow() {
        if ($(this).val() != "Run method with parameters to see...")
            window.open($(this).val(), '_newtab');
    }

    function toggleRunMethodBox() {
        if ($(this).hasClass('up-button')) {
            $(this).removeClass('up-button').addClass('down-button');
            $('#methodParams').slideUp();
        }
        else if ($(this).hasClass('down-button')) {
            $(this).removeClass('down-button').addClass('up-button');
            $('#methodParams').slideDown();
        }

    }

    function makeMethodCall() {
        var callString = getSerializedFormString('#methodOptions');
        $('#methodCallString').val(getBaseUrl() + '/m/usp_udt_' + methodname + '?' + callString);
        // Show loading animation
        showLoadingAnimation($('#grid-container .grid-items-list'));
        getData('/m/' + fullmethodname, callString, insertData, false, '', $('#grid-container .grid-items-list'), true);
        return false;
    }

        function insertMethodParameters(data) {
        if (data == "no data") {
            return;
        }
        else if (data.error) {
            $('#methodParams').append('Error: "' + data.error + '"');
        }
        else {

            $.each(data, function (i, item) {
                var entryClass = "";
                if (item.required == "True") {
                    entryClass += " required";
                }
                insertParamEntry(item.Name, entryClass, item.Name, getHtmlInputTypeFromSqlDatatype(item.Type));
            });
        }
    }

        function insertParamEntry(displayName, entryClass, entryName, entryType) {
        var newEntry = methodParamHtml.replace(/##displayname##/g, displayName);
        newEntry = newEntry.replace(/##class##/g, entryClass);
        newEntry = newEntry.replace(/##inputname##/g, $.trim(entryName));
        newEntry = newEntry.replace(/##type##/g, entryType);

        $('#methodParams').append(newEntry);
    }
</script>
<form id="methodOptions">
    <div class="method-buttons">
        <div id="hideShowButton" class="grey-item grey-button up-button image-button" tip="Refresh">Hide</div>
        <input id="methodCallString" type="text" class="" value="Run method with parameters to see..." title="Double click to open in new tab" readonly="readonly"/>
        <input class="grey-item grey-button play-button image-button" tip="Refresh" type="submit" />
    </div>
    <div id="methodParams" class="method-parameters"></div>
</form>

<div>
@{Html.RenderPartial("EpicGrid", null);}
</div>